//
//  XSJCategoryJSVC.m
//  XSJ
//
//  Created by yuanyuan he on 2020/4/28.
//  Copyright © 2020 apple. All rights reserved.
//

#import "XSJCategoryJSVC.h"
#import "JXCategoryTitleView.h"
#import "RefreshGifHeader.h"
#import "SearchToolBarView.h"
#import "PYSearchViewController.h"

@interface XSJCategoryJSVC ()

@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;

@property (strong , nonatomic)SearchToolBarView * searchView;

@property (nonatomic, strong) UIButton *newsButton;
@property (nonatomic, strong) UIButton *searchButton;

@end

@implementation XSJCategoryJSVC

- (void)viewDidLoad {
    if (self.titles == nil) {
        self.titles = @[@"推荐", @"学习",@"课堂",@"娱乐",];
    }
    
    [super viewDidLoad];
    
    self.myCategoryView.titles = self.titles;
    self.myCategoryView.titleSelectedColor = ColorHex(0x9B5DFF);
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
    lineView.indicatorColor = ColorHex(0x9B5DFF);
    self.myCategoryView.indicators = @[lineView];
    
    
    //导航栏中间标题
    UIView *naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LL_ScreenWidth, LL_NavigationBarHeight)];
    self.navigationItem.titleView = naviView;
    if (@available(iOS 11.0, *)) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [naviView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(50);
                make.right.mas_equalTo(-50);
                make.top.bottom.mas_equalTo(0);
            }];
        });
    }
    
    [naviView addSubview:self.searchView];
    [self.view addSubview:self.newsButton];
    [self.view addSubview:self.searchButton];
    
    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.centerX.equalTo(naviView);
        make.width.mas_equalTo(255);
        make.height.mas_equalTo(24);
    }];
    
    [_newsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self);
    }];
    
    [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->_newsButton.mas_left).offset(-10);
        make.centerY.equalTo(self->_newsButton);
    }];
}

-(void)newsButtonDidClick
{
//    YCHHotHouseListJSVC* vc = [[YCHHotHouseListJSVC alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (UIButton *)newsButton {
    if (_newsButton == nil) {
        _newsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_newsButton setImage:[UIImage imageNamed:@"qiandao"] forState:UIControlStateNormal];
        [_newsButton addTarget:self action:@selector(newsButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _newsButton;
}

- (UIButton *)searchButton {
    if (_searchButton == nil) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchButton setImage:[UIImage imageNamed:@"sousuo1"] forState:UIControlStateNormal];
        [_searchButton addTarget:self action:@selector(searchButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (JXCategoryTitleView *)myCategoryView {
    return (JXCategoryTitleView *)self.categoryView;
}

- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}

//categoryView布局
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.categoryView.frame = CGRectMake(0, LL_StatusBarHeight, kWidthValue(150), [self preferredCategoryViewHeight]);
    
    self.listContainerView.frame = CGRectMake(0, [self preferredCategoryViewHeight]+LL_StatusBarHeight, self.view.bounds.size.width, self.view.bounds.size.height);
}

#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (id<JXCategoryListContentViewDelegate>)preferredListAtIndex:(NSInteger)index
{
    if (index==0)
    {
//        YCHHomeVC* vc = [[YCHHomeVC alloc] init];
//        vc.naviCategoryController = self.navigationController;
//        return vc;
    }
    else if(index==1)
    {
//        YCHConcernedVC* vc = [[YCHConcernedVC alloc] init];
//        vc.naviCategoryController = self.navigationController;
//        return vc;
    }
    else if (index==2)
    {
        
    }
    else if (index==3)
    {
        
    }
    
    return nil;
}

- (void)handleSingleTap:(UITouch *)touch
{
    //弹搜索
    // 1. Create hotSearches array
    NSArray *hotSeaches = @[];
    // 2. Create searchViewController
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"周星驰" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        // Call this Block when completion search automatically
        // Such as: Push to a view controller
        // 如：跳转到指定控制器
//        YCHSearchMoreVC* vc = [[YCHSearchMoreVC alloc]init];
//        vc.text = searchText;
//        [searchViewController.navigationController pushViewController:vc animated:YES];
        
    }];
    // 3. present the searchViewController
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];
    
}


-(SearchToolBarView *)searchView
{
    if (_searchView==nil) {
        _searchView =  [[SearchToolBarView alloc]init];
        _searchView.layer.cornerRadius = 12.0f;
        
        //点击事件
        _searchView.userInteractionEnabled = YES;
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [_searchView addGestureRecognizer:singleTap];
    }
    return _searchView;
}



@end
